home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / asmutil / afloat.zip / FTOE.ASM < prev    next >
Assembly Source File  |  1988-03-14  |  4KB  |  193 lines

  1.         PAGE ,132
  2. ;----------------------------------------------------------
  3. ; FTOE -- version for use with assembly language programs
  4. ;
  5. ; Copyright Bob Kline 1988
  6. ;
  7. ; Purpose:
  8. ;    Convert floating point value to ASCII string using
  9. ;    scientific notation.
  10. ;
  11. ; Input:
  12. ;    DX:AX contain 4-byte real
  13. ;    DI points to space for new string
  14. ;    CX specifies precision
  15. ;
  16. ; Output:
  17. ;       String stored at location pointed to by DI
  18. ;
  19. ; Registers changed:
  20. ;       AX, BX, CX, DX, DI, SI
  21. ;
  22. ; Other procedures called:
  23. ;    LDIV10
  24. ;    FBINTODEC
  25. ;
  26. ; Comments:
  27. ;    Calling procedure is responsible for making sure
  28. ;       that there is enough space for the new string.
  29. ;----------------------------------------------------------
  30.  
  31.     PUBLIC    FTOE
  32.  
  33.     EXTRN    FBINTODEC:NEAR
  34.     EXTRN    LDIV10:NEAR
  35.  
  36.     .MODEL SMALL
  37.  
  38. STRING        EQU    BYTE PTR [DI]
  39. POINTER     EQU    DI
  40. MAXPREC     EQU    7
  41. PRECISION    EQU    CX
  42. EXPONENT    EQU    BX
  43. INDEX        EQU    SI
  44. DIGITS        EQU    DX
  45.  
  46.     .DATA?
  47. TEMPSTRING    DB    11 DUP(?)
  48.  
  49.     .CODE
  50.  
  51. FTOE    PROC
  52.  
  53. ; at the outset DX:AX = 4-byte real, DI points to start of output
  54. ;   string, and CX has the specified precision to use
  55.  
  56. ; bring precision into acceptable range
  57.     CMP    PRECISION,MAXPREC
  58.         JLE     L0
  59.     MOV    PRECISION,MAXPREC
  60.         JMP     SHORT L1
  61. L0:     OR      PRECISION,PRECISION
  62.         JNS     L1
  63.     XOR    PRECISION,PRECISION
  64.  
  65. ;   take care of zero as a special case by moving
  66. ;   zeros into the temporary string, and jumping to
  67. ;   the part where we move from the temporary string
  68. ;   into the final output string; we put -7 into
  69. ;   EXPONENT because the final exponent is calculated
  70. ;   by the formula EXPONENT + DIGITS - 1, and we
  71. ;   want it to come out as zero, right?
  72. L1:     OR      AX,AX
  73.     JNZ    L3
  74.     OR    DX,DX
  75.     JNZ    L3
  76.     PUSH    PRECISION
  77.     MOV    CX,8
  78.         XOR     INDEX,INDEX
  79. L2:     MOV     TEMPSTRING[SI],'0'
  80.     INC    SI
  81.     LOOP    L2
  82.     POP    PRECISION
  83.         MOV     EXPONENT,-7
  84.     MOV    DIGITS,8
  85.     JMP    SHORT L9A
  86.  
  87. ; call fbintodec, which comes back with mantissa in DX:AX,
  88. ;   sign in low bit of CX, and exponent in BX -- we'll check
  89. ;   the sign immediately and take care of it so that CX will
  90. ;   be free to pop the precision back off the stack
  91. L3:    PUSH    PRECISION
  92.     PUSH    POINTER
  93.     CALL    FBINTODEC
  94.     POP    POINTER
  95.     OR    CX,CX
  96.     JZ    L4
  97.     MOV    STRING,'-'
  98.     INC    POINTER
  99. L4:
  100.  
  101. ; break down mantissa into a temporary string, using SI as the
  102. ;   index to the string: note that the digits of the string will
  103. ;   be stored backwards, that is, least significant first, so that
  104. ;   when we go to move the digits into the output string, we'll
  105. ;   pick them off from the end and work back to the beginning.
  106. ;   The algorithm here is:
  107. ;    while the mantissa is not zero
  108. ;        divide by 10 and move the remainder into the string
  109. L7:    XOR    INDEX,INDEX
  110.     PUSH    EXPONENT
  111.     JMP    SHORT L9
  112. L10:    CALL    LDIV10
  113.     OR    BL,30h
  114.     MOV    TEMPSTRING[INDEX],BL
  115.     INC    INDEX
  116. L9:    OR    AX,AX
  117.     JNZ    L10
  118.     OR    DX,DX
  119.     JNZ    L10
  120.     POP    EXPONENT
  121.     POP    PRECISION
  122.  
  123. ; round the mantissa if we need to
  124.     MOV    DIGITS,INDEX
  125.     SUB    INDEX,PRECISION
  126.     DEC    INDEX
  127.     DEC    INDEX
  128.     JL    L9A
  129.     CMP    TEMPSTRING[INDEX],'4'
  130.     JLE    L9A
  131.     INC    INDEX
  132. L11C:    CMP    TEMPSTRING[INDEX],'9'
  133.     JE    L11A
  134.     INC    TEMPSTRING[INDEX]
  135.     JMP    SHORT L9A
  136. L11A:    MOV    TEMPSTRING[INDEX],'0'
  137. L11:    INC    INDEX
  138.     CMP    INDEX,DIGITS
  139.     JL    L11C
  140.     INC    DIGITS
  141.     INC    INDEX
  142.     MOV    TEMPSTRING[INDEX],'1'
  143.  
  144. ; get ready for some string moves
  145. L9A:    CLD
  146.     MOV    INDEX,DIGITS
  147.     DEC    INDEX
  148.  
  149. ; move one digit, then add a decimal point, followed by the rest
  150. ;   of the digits; don't overshoot the precision -- on the other
  151. ;   hand, if there aren't enough digits for the precision, fill
  152. ;   up with zeros on the right
  153.     MOV    AL,TEMPSTRING[INDEX]
  154.     STOSB
  155.     OR    PRECISION,PRECISION
  156.     JZ    L12
  157.     MOV    AL,'.'
  158. L13:    STOSB
  159.     DEC    SI
  160.     JS    L13A
  161.     MOV    AL,TEMPSTRING[INDEX]
  162.         JMP     SHORT L13B
  163. L13A:    MOV    AL,'0'
  164. L13B:   LOOP    L13
  165.  
  166. ; adjust the exponent: (exponent = exponent + digits - 1) and move
  167. ;   it into the string
  168. L12:    ADD    EXPONENT,DIGITS
  169.     DEC    EXPONENT
  170.     MOV    AL,'e'
  171.     STOSB
  172.     MOV    AL,'+'
  173.     JGE    L12A
  174.     MOV    AL,'-'
  175.     NEG    EXPONENT
  176. L12A:    STOSB
  177.     MOV    AX,EXPONENT
  178.     MOV    CL,10
  179.     DIV    CL
  180.     OR    AX,3030h
  181.     STOSB
  182.     XCHG    AH,AL
  183.     STOSB
  184.  
  185. ; terminate the string with a null byte and return from the procedure
  186.     MOV    AL,0
  187.     STOSB
  188.     RET
  189.  
  190. FTOE    ENDP
  191.  
  192.         END
  193.